<template>
    <div class="category">
        <Header></Header>
        <div class="mb20 fr mr30">
            <el-button type="primary" @click="openDialog">添加分类</el-button>
        </div>
        <el-table
                :data="categoryList"
                border
                style="width: 100%">
            <el-table-column
                    prop="_id"
                    label="编号">
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="名称">
            </el-table-column>
            <el-table-column
                    label="操作">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="openEdit(scope.row._id)">编辑</el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.row._id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-dialog title="添加分类" :visible.sync="dialogAdd" @close="handleClose">
            <el-form :model="form" label-width="80px">
                <el-form-item label="活动名称">
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogAdd = false">取 消</el-button>
                <el-button type="primary" @click="handleAdd()">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="修改分类" :visible.sync="dialogEdit" @close="handleClose">
            <el-form :model="form" label-width="80px">
                <el-form-item label="活动名称">
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogEdit = false">取 消</el-button>
                <el-button type="primary" @click="handleEdit()">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import Header from "../components/Header"
    export default {
        name: "Category",
        components:{
            Header
        },
        data(){
            return{
                categoryList:[],
                form:{
                    name:''
                },
                dialogAdd:false,
                dialogEdit:false,
                id:''
            }
        },
        mounted() {
            this.getList()
        },
        methods:{
            getList(){
                this.get(this.$api.categoryModel.getCategory)
                   .then(res=>{
                       this.categoryList = res.result
                   })
            },
            openDialog(){
                this.dialogAdd = true
            },
            handleClose(){
             this.form.name=''
            },
            handleAdd(){
                this.post(this.$api.categoryModel.categoryAdd,this.form)
                    .then(res=>{
                        console.log(res)
                        this.categoryList.push(res.result)
                        this.dialogAdd = false
                        // this.getList()
                    })
            },
            openEdit(id){
                this.dialogEdit = true
                this.id = id
                this.getInfo()
            },
            getInfo(){
                this.get(this.$api.categoryModel.categoryInfo,{
                    id:this.id
                }).then(res=>{
                    this.form = res.result
                })
            },
            handleEdit(){
                this.post(this.$api.categoryModel.categoryUpdate,{
                    id:this.id,
                    name:this.form.name
                })
                .then(res=>{
                    console.log(res);
                })
            },
             handleDelete(id){
                let that = this
                let userId =id
                 this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
                     confirmButtonText: '确定',
                     cancelButtonText: '取消',
                     type: 'warning'
                 }).then(() => {
                     that.post(this.$api.categoryModel.categoryDelete,{
                         id:id
                     })
                         .then(res=>{
                             that.categoryList.forEach((item,index)=>{
                                 if (item._id===userId){
                                     that.categoryList.splice(index,1)
                                 }
                             })
                         })
                 }).catch(() => {
                     this.$message({
                         type: 'info',
                         message: '已取消删除'
                     });
                 });
             }
        }
    }
</script>

<style lang="stylus" scoped>
.category
    width 100%

</style>
